import 'package:valovault_reminder/common/ui/val_auto_fit_screen_size.dart';
import 'package:flutter/cupertino.dart';
import 'val_ui_kit_btn.dart';
import 'val_ui_kit_base_colors.dart';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'val_ui_kit_base_decoration.dart';

class ValUIKitBaseDivider extends StatelessWidget {
  const ValUIKitBaseDivider({
    Key? key,
    this.color = ValUIKitBaseColors.line,
    this.height = 0.5,
    this.marginLeft = 0,
    this.marginRight = 0,
  }) : super(key: key);

  const ValUIKitBaseDivider.bold({
    Key? key,
    this.color = ValUIKitBaseColors.whiteF8,
    this.height = 8,
    this.marginLeft = 0,
    this.marginRight = 0,
  }) : super(key: key);

  const ValUIKitBaseDivider.margin({
    Key? key,
    this.color = ValUIKitBaseColors.line,
    this.height = 0.5,
    this.marginLeft = 16,
    this.marginRight = 16,
  }) : super(key: key);

  final Color color;
  final double height;
  final double marginLeft;
  final double marginRight;

  @override
  Widget build(BuildContext context) {
    return Container(
      color: color,
      height: height.w,
      margin: EdgeInsets.only(
        left: marginLeft.w,
        right: marginRight.w,
      ),
    );
  }
}

class CommonListItemScaffold extends StatelessWidget {
  const CommonListItemScaffold({
    this.left,
    this.center,
    this.right,
    this.bottom,
    this.onTap,
    this.crossAxisAlignment = CrossAxisAlignment.center,
    this.mainAxisAlignment = MainAxisAlignment.start,
    this.onLongPress,
    this.mainAxisSize = MainAxisSize.max,
    this.contentPadding,
    this.hasInkWell = true,
    Key? key,
  }) : super(key: key);

  final Widget? left;
  final Widget? center;
  final Widget? right;
  final Widget? bottom;
  final Function()? onTap;
  final CrossAxisAlignment crossAxisAlignment;
  final MainAxisAlignment mainAxisAlignment;
  final Function()? onLongPress;
  final MainAxisSize mainAxisSize;
  final EdgeInsetsGeometry? contentPadding;
  final bool hasInkWell;

  @override
  Widget build(BuildContext context) {
    var topChildren = <Widget>[];
    if (left != null) topChildren.add(left!);
    if (center != null) topChildren.add(center!);
    if (right != null) topChildren.add(right!);
    var top = Row(
      mainAxisSize: mainAxisSize,
      crossAxisAlignment: crossAxisAlignment,
      mainAxisAlignment: MainAxisAlignment.start,
      children: topChildren,
    );

    Widget content = top;
    if (bottom != null) {
      content = Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [top, bottom!],
      );
    }

    return onTap == null && onLongPress == null
        ? Container(
      padding: contentPadding,
      child: content,
    )
        : ValUIKitBtn.item(
      content,
      onTap ?? () {},
      padding: contentPadding,
      onLongPress: onLongPress,
      hasInkWell: hasInkWell,
    );
  }
}

///通用的网络请求提示
class CenterProgress extends StatelessWidget {
  const CenterProgress({Key? key, this.progress}) : super(key: key);

  final ValueNotifier<double>? progress;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.all(20.w),
        width: 80.w,
        height: 80.w,
        decoration: ValUIKitBaseDecoration.getRadius(
          bg: Colors.black87,
          all: 10.w,
        ),
        child: progress != null
            ? ValueListenableBuilder(
          valueListenable: progress!,
          builder: (BuildContext context, double value, Widget? child) {
            return getProgress(progress: value);
          },
        )
            : getProgress(),
      ),
    );
  }

  Widget getProgress({double? progress}) {
    return CircularProgressIndicator(
      value: progress,
      color:  Colors.white,
    );
  }
}

///虚线描边控件
class DashBorder extends StatelessWidget {
  DashBorder(
      this.child, {
        Key? key,
        CustomPainter? paint,
        double? defaultWidth,
        Color? defaultColor,
        double? defaultDashWidth,
        double? defaultDashGap,
      })  : paint = paint ??
      _DashBorderPaint(
        defaultWidth ?? 1.w,
        defaultColor ?? Colors.black,
        defaultDashWidth ?? 3.w,
        defaultDashGap ?? 3.w,
      ),
        super(key: key);
  final Widget child;
  final CustomPainter? paint;

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: paint,
      child: child,
    );
  }
}

class _DashBorderPaint extends CustomPainter {
  _DashBorderPaint(this.width, this.color, this.dashWidth, this.dashGap);

  final Color color;
  final double dashWidth;
  final double dashGap;
  final double width;

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = color
      ..strokeWidth = width
      ..strokeCap = StrokeCap.round
      ..isAntiAlias = true
      ..style = PaintingStyle.stroke;

    Path path = Path();
    Path paintPath = Path();
    path.addRect(Rect.fromLTWH(0, 0, size.width, size.height));
    double distance = 0;
    for (PathMetric pathMetric in path.computeMetrics()) {
      while (distance < pathMetric.length) {
        paintPath.addPath(pathMetric.extractPath(distance, distance + dashWidth), Offset.zero);
        distance += dashWidth + dashGap;
      }
    }
    canvas.drawPath(paintPath, paint);
  }

  @override
  bool shouldRepaint(_DashBorderPaint oldDelegate) {
    return color != oldDelegate.color ||
        dashWidth != oldDelegate.dashWidth ||
        dashGap != oldDelegate.dashGap ||
        width != oldDelegate.width;
  }
}

class CommonDropPointer extends StatelessWidget {
  CommonDropPointer(
      this.absorbing, {
        Key? key,
        this.child,
      }) : super(key: key);

  final bool Function() absorbing;
  final Widget? child;
  final drop = [false];

  @override
  Widget build(BuildContext context) {
    return Listener(
      onPointerCancel: (_) {
        drop[0] = false;
      },
      onPointerUp: (_) {
        drop[0] = false;
      },
      child: CommonAbsorbPointer(
            () {
          bool b = absorbing();
          if (b) {
            drop[0] = true;
          }
          return drop[0] || b;
        },
        child: child,
      ),
    );
  }
}

class CommonAbsorbPointer extends SingleChildRenderObjectWidget {
  const CommonAbsorbPointer(
      this.absorbing, {
        Key? key,
        Widget? child,
      }) : super(key: key, child: child);

  final bool Function() absorbing;

  @override
  _RenderCommonAbsorbPointer createRenderObject(BuildContext context) {
    return _RenderCommonAbsorbPointer(absorbing);
  }

  @override
  void updateRenderObject(BuildContext context, _RenderCommonAbsorbPointer renderObject) {
    renderObject.absorbing = absorbing;
  }
}

class _RenderCommonAbsorbPointer extends RenderProxyBox {
  _RenderCommonAbsorbPointer(
      bool Function() _absorbing, {
        RenderBox? child,
      })  : _absorbing = _absorbing,
        super(child);

  bool Function() get absorbing => _absorbing;

  bool Function() _absorbing;

  set absorbing(bool Function() value) {
    if (_absorbing == value) return;
    _absorbing = value;
  }

  @override
  bool hitTest(BoxHitTestResult result, {required Offset position}) {
    return absorbing() ? size.contains(position) : super.hitTest(result, position: position);
  }
}

///间距组件
class CommonMargin extends StatelessWidget {
  const CommonMargin({Key? key, this.width, this.height}) : super(key: key);

  const CommonMargin.width(this.width, {Key? key})
      : height = null,
        super(key: key);

  const CommonMargin.height(this.height, {Key? key})
      : width = null,
        super(key: key);

  final double? width;
  final double? height;

  @override
  Widget build(BuildContext context) {
    return SizedBox(width: width, height: height);
  }
}

class CommonItemBean {
  CommonItemBean({
    required this.title,
    this.content,
    this.icon,
    this.contentWidget,
  });

  final String title;
  final String? icon;
  final String? content;
  final Widget? contentWidget;
}

///通用TabBar
class CommonTabBar extends StatelessWidget {
  const CommonTabBar({
    Key? key,
    required this.titles,
    this.pageStorageKey,
    this.titleWidget,
    this.fontSize = 15,
    this.indicatorColor = ValUIKitBaseColors.theme,
    this.tabController,
    this.tabIndicatorInsets,
    this.height,
    this.unselectedLabelColor = ValUIKitBaseColors.textLeveOne,
    this.isScrollable = false,
    this.labelPadding = EdgeInsets.zero,
    this.labelColor = ValUIKitBaseColors.theme,
    this.backgroundColor,
  }) : super(key: key);

  const CommonTabBar.customer(
      this.pageStorageKey, {
        Key? key,
        required this.titleWidget,
        this.titles = const [],
        this.fontSize = 15,
        this.indicatorColor = ValUIKitBaseColors.theme,
        this.tabController,
        this.tabIndicatorInsets,
        this.height,
        this.unselectedLabelColor = ValUIKitBaseColors.textLeveOne,
        this.isScrollable = false,
        this.labelPadding = EdgeInsets.zero,
        this.labelColor = ValUIKitBaseColors.theme,
        this.backgroundColor,
      }) : super(key: key);

  final List<String> titles;
  final List<Widget>? titleWidget;
  final double? fontSize;
  final PageStorageKey? pageStorageKey;
  final Color indicatorColor;
  final TabController? tabController;
  final EdgeInsets? tabIndicatorInsets;
  final double? height;
  final Color unselectedLabelColor;
  final bool isScrollable;
  final EdgeInsets? labelPadding;
  final Color? labelColor;
  final Color? backgroundColor;

  @override
  Widget build(BuildContext context) {
    return Container(
      height: height ?? 45.w,
      alignment: Alignment.topLeft,
      color: backgroundColor,
      child: TabBar(
        controller: tabController,
        key: pageStorageKey,
        labelPadding: labelPadding,
        isScrollable: isScrollable,
        indicator: CommonTabIndicator(
          borderSide: BorderSide(width: 2.w, color: indicatorColor),
          insets: tabIndicatorInsets ?? EdgeInsets.fromLTRB(3.w, 0, 3.w, 0),
        ),
        indicatorSize: TabBarIndicatorSize.label,
        labelColor: labelColor,
        labelStyle: TextStyle(fontSize: fontSize, fontWeight: FontWeight.w600),
        unselectedLabelColor: unselectedLabelColor,
        unselectedLabelStyle: TextStyle(fontSize: fontSize),
        tabs: titleWidget ?? titles.map((e) => Tab(text: e)).toList(),
      ),
    );
  }
}
